package com.example.sunmiv1.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.StatisticsVipBean;
import com.example.sunmiv1.views.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StatisticsVipAdapter extends BaseAdapter {

    private Context context;
    private List<StatisticsVipBean.DataBean.ListBean> vipList;
    private ViewHolder viewHolder;

    public StatisticsVipAdapter(Context context, List<StatisticsVipBean.DataBean.ListBean> vipList) {
        this.context = context;
        this.vipList = vipList;
    }

    @Override
    public int getCount() {
        return vipList.size();
    }

    @Override
    public Object getItem(int position) {
        return vipList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.statistics_vip_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        StatisticsVipBean.DataBean.ListBean listBean = vipList.get(position);
//        viewHolder.statisticsVipAdaNum.setText((position+1)+"");
        RoundedCorners roundedCorners = new RoundedCorners(45);
        //通過RequestOptions擴展功能,override:採樣率,因爲ImageView就這麼大,可以壓縮圖片,降低內存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        options.placeholder(R.mipmap.default_img);//佔位圖  .error(R.mipmap.error_food)//如果是錯誤的情況
        Glide.with(context)
                .load(listBean.getHeadimgurl())
                .apply(options)
                .into(viewHolder.statisticsVipAdaImage);
        String name = "";
        if (TextUtils.isEmpty(listBean.getName())) {
            name += "——";
        } else {
            name += listBean.getName();
        }
        name += "|";
        if (TextUtils.isEmpty(listBean.getNickname())) {
            name += "——";
        } else {
            name += listBean.getNickname();
        }
        viewHolder.statisticsVipAdaName.setText(name);
//        viewHolder.statisticsVipAdaPhone.setText(listBean.getMobile_phone());
//        viewHolder.statisticsVipAdaType.setText(listBean.getVip_type());
//        viewHolder.statisticsVipAdaGrade.setText(listBean.getVip_lv());
//        viewHolder.statisticsVipAdaTime.setText(listBean.getExpense_last());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.statisticsVip_ada_image)
        CircleImageView statisticsVipAdaImage;
        @BindView(R.id.statisticsVip_ada_name)
        TextView statisticsVipAdaName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
